package com.qiantu.phone.ui.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.entity.SmsBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.utils.StringUtil;

import java.util.HashMap;
import java.util.Map;

public class EnterPhoneActivity extends AppActivity {

    private android.widget.TextView mName;
    private com.qiantu.phone.widget.EditTextLayout mPhone;
    private android.widget.TextView mPhoneCannotUse;
    private android.widget.TextView mBtn;
    private TextView mErrorHint;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_enter_phone;
    }

    @Override
    protected void initView() {
        mName = findViewById(R.id.name);
        mPhone = findViewById(R.id.phone);
        mErrorHint = findViewById(R.id.phone_error_hint);
        mPhoneCannotUse = findViewById(R.id.phone_cannot_use);
        mBtn = findViewById(R.id.btn);
        setOnClickListener(mPhoneCannotUse, mBtn);
        mPhone.getEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 11){
                    if (StringUtil.isPhoneLegal(s.toString())){
                        mBtn.setEnabled(true);
                        hideKeyboard(mPhone);
                    }else {
                        mPhone.setSelected(true);
                        mBtn.setEnabled(false);
                        mErrorHint.setText(R.string.phone_error_hint);
                    }
                }else {
                    mBtn.setEnabled(false);
                    mPhone.setSelected(false);
                    mErrorHint.setText("");
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == mPhoneCannotUse){
            PhoneAppealActivity.start(this);
        }else if (view == mBtn){
            Map<String, Object> data = new HashMap<>();
            data.put("mobile", mPhone.getText().toString());
            data.put("smsType", "modifyPassword_boyun");
            LLHttpManager.sendSms(this, data, new HttpCallback<HttpData<SmsBean>>(this){
                @Override
                public void onSucceed(HttpData<SmsBean> data) {
                    if (data.isRequestSucceed()) {
                        toast(R.string.sms_code_send);
                        postDelayed(() -> {
                            // 跳转到验证码界面
                            //验证老手机号，传如原smsId
                            AppApplication.getInstance().setPhone(mPhone.getText().toString());
                            Captcha3Activity.start(getAppActivity(), AppApplication.getInstance().getUserPhone(), "modifyPassword", data.getData().getSmsID());
                        }, 600);
                    }
                }
            });
        }
    }
}